import sys
input = sys.stdin.buffer.readline
def find_root(root_dict, x):
L = []
while x != root_dict[x]:
L.append(x)
x = root_dict[x]
for y in L:
root_dict[y] = x
return x
def process(n, G):
m = len(G)
answer = [None for i in range(m)]
root_dict = [i for i in range(n+1)]
good_edges = []
bad_edges = []
for i in range(m):
u, v = G[i]
u1 = find_root(root_dict, u)
v1 = find_root(root_dict, v)
if u1 != v1:
good_edges.append([u, v, i])
root_dict[u1] = v1
answer[i] = '0'
else:
bad_edges.append([u, v, i])
answer[i] = '1'
if len(bad_edges)==3:
u1, v1, i1 = bad_edges[0]
u2, v2, i2 = bad_edges[1]
u3, v3, i3 = bad_edges[2]
L = [u1, v1, u2, v2, u3, v3]
L = sorted(L)
if L[0]==L[1] and L[2]==L[3] and L[4]==L[5]:
answer[i3] = '0'
root_dict = [i for i in range(n+1)]
root_dict[u3] = v3
for u, v, i in good_edges:
u1 = find_root(root_dict, u)
v1 = find_root(root_dict, v)
if u1==v1:
answer[i] = '1'
break
else:
root_dict[u1] = v1
answer = ''.join(answer)
sys.stdout.write(f'{answer}\n')
return
t = int(input())
for i in range(t):
n, m = [int(x) for x in input().split()]
G = []
for j in range(m):
u, v = [int(x) for x in input().split()]
G.append([u, v])
process(n, G)
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define endl '\n'
#define int long long
#define lowbit(n) (n&(-n))
#define db long double
#define pdd pair<double, double>
#define pii pair<int, int>
#define x first
#define y second
#define ll long long
const int N = 2e5 + 10, mod = 998244353;
int n, m, x, p[N];
pii a[N];
int find(int x) {
if (x != p[x])p[x] = find(p[x]);
return p[x];
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++)p[i] = i;
set<int> se;
pii tem;
string s(m, '0');
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
a[i] = { u,v };
int fu = find(u), fv = find(v);
if (fu == fv)s[i] = '1', se.insert(u), se.insert(v), tem = a[i];
else p[fu] = p[fv];
}
if (se.size() == 3 && m == n + 2) {
for (int i = 0; i < m; i++)
if (a[i] == tem) {
s[i] = '0';
for (int j = 0; j < m; j++) {
if (i != j && (tem.x == a[j].x || tem.x == a[j].y))
s[j] = '1';
}
}
}
cout << s << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T = 1; cin >> T;
while (T--) solve();
}
580A- Kefa and First Steps | 1472B- Fair Division |
996A - Hit the Lottery | MSNSADM1 Football |
MATCHES Playing with Matches | HRDSEQ Hard Sequence |
DRCHEF Doctor Chef | 559. Maximum Depth of N-ary Tree |
821. Shortest Distance to a Character | 1441. Build an Array With Stack Operations |
1356. Sort Integers by The Number of 1 Bits | 922. Sort Array By Parity II |
344. Reverse String | 1047. Remove All Adjacent Duplicates In String |
977. Squares of a Sorted Array | 852. Peak Index in a Mountain Array |
461. Hamming Distance | 1748. Sum of Unique Elements |
897. Increasing Order Search Tree | 905. Sort Array By Parity |
1351. Count Negative Numbers in a Sorted Matrix | 617. Merge Two Binary Trees |
1450. Number of Students Doing Homework at a Given Time | 700. Search in a Binary Search Tree |
590. N-ary Tree Postorder Traversal | 589. N-ary Tree Preorder Traversal |
1299. Replace Elements with Greatest Element on Right Side | 1768. Merge Strings Alternately |
561. Array Partition I | 1374. Generate a String With Characters That Have Odd Counts |